home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / TaggedIntPropertyEditor.java < prev    next >
Text File  |  1998-04-21  |  4KB  |  156 lines

  1. package com.symantec.itools.beans;
  2.  
  3. import java.beans.*;
  4.  
  5. public class TaggedIntPropertyEditor
  6.     extends PropertyEditorSupport
  7. {
  8.     static protected class TaggedInt
  9.     {
  10.         public TaggedInt(String tag, int value, String initializationString)
  11.         {
  12.             m_Tag = tag;
  13.             m_Value = value;
  14.             m_InitializationString = initializationString;
  15.         }
  16.         
  17.         public String getTag()
  18.         {
  19.             return m_Tag;
  20.         }
  21.         
  22.         public int getValue()
  23.         {
  24.             return m_Value;
  25.         }
  26.         
  27.         public String getInitializationString()
  28.         {
  29.             return m_InitializationString;
  30.         }
  31.         
  32.         protected String    m_Tag;
  33.         protected int        m_Value;
  34.         protected String    m_InitializationString;
  35.     }
  36.     
  37.     private TaggedIntPropertyEditor()
  38.     {
  39.     }
  40.     
  41.     public TaggedIntPropertyEditor(TaggedInt[] tags)
  42.     {
  43.         this.m_Tags = tags;
  44.     }
  45.     
  46.     /**
  47.      * If the property value must be one of a set of known tagged values, 
  48.      * then this method should return an array of the tag values.  This can
  49.      * be used to represent (for example) enum values.  If a PropertyEditor
  50.      * supports tags, then it should support the use of setAsText with
  51.      * a tag value as a way of setting the value.
  52.      *
  53.      * @return The tag values for this property.  May be null if this 
  54.      *   property cannot be represented as a tagged value.
  55.      *    
  56.      */
  57.     public String[] getTags()
  58.     {
  59.         String[] tags = new String[m_Tags.length];
  60.         
  61.         for (int i = 0;i < m_Tags.length;i++)
  62.             tags[i] = m_Tags[i].getTag();
  63.         
  64.         return tags;
  65.     }
  66.     
  67.     /**
  68.      * @return The property value as a string suitable for presentation
  69.      *       to a human to edit.
  70.      * <p>   Returns "null" is the value can't be expressed as a string.
  71.      * <p>   If a non-null value is returned, then the PropertyEditor should
  72.      *         be prepared to parse that string back in setAsText().
  73.      */
  74.     public String getAsText()
  75.     {
  76.         return getValueAsString(true);
  77.     }
  78.  
  79.     /**
  80.      * Set the property value by parsing a given String.  May raise
  81.      * java.lang.IllegalArgumentException if either the String is
  82.      * badly formatted or if this kind of property can't be expressed
  83.      * as text.
  84.      * @param text  The string to be parsed.
  85.      */
  86.     public void setAsText(String text)
  87.         throws java.lang.IllegalArgumentException
  88.     {
  89.         //Search for matching tag
  90.         for (int i = 0;i < m_Tags.length;i++)
  91.         {
  92.             TaggedInt currTag = m_Tags[i];
  93.             
  94.             if (currTag.getTag().equals(text))
  95.             {
  96.                 setValueAsInteger(new Integer(currTag.getValue()));
  97.                 
  98.                 return;
  99.             }
  100.         }
  101.         
  102.         //Call super - it throws
  103.         super.setAsText(text);
  104.     }
  105.  
  106.     protected void setValueAsInteger(Integer newValue)
  107.     {
  108.         setValue(newValue);
  109.     }
  110.     
  111.     /**
  112.      * This method is intended for use when generating Java code to set
  113.      * the value of the property.  It should return a fragment of Java code
  114.      * that can be used to initialize a variable with the current property
  115.      * value.
  116.      * <p>
  117.      * Example results are "2", "new Color(127,127,34)", "Color.orange", etc.
  118.      *
  119.      * @return A fragment of Java code representing an initializer for the
  120.      *       current value.
  121.      */
  122.     public String getJavaInitializationString()
  123.     {
  124.         return getValueAsString(false);
  125.     }
  126.     
  127.     protected Integer getValueAsInteger()
  128.     {
  129.         return (Integer)getValue();
  130.     }
  131.     
  132.     protected String getValueAsString(boolean tag)
  133.     {
  134.         Integer value = getValueAsInteger();
  135.         
  136.         if (value != null)
  137.         {
  138.             int intValue = value.intValue();
  139.             
  140.             //Search for matching tag
  141.             for (int i = 0;i < m_Tags.length;i++)
  142.             {
  143.                 TaggedInt currTag = m_Tags[i];
  144.                 if (currTag.getValue() == intValue)
  145.                 {
  146.                     return tag ? currTag.getTag() : currTag.getInitializationString();
  147.                 }
  148.             }
  149.         }
  150.         
  151.         return "";
  152.     }
  153.     
  154.     protected TaggedInt[] m_Tags;
  155. }
  156.